//
//  AddNewsViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/10.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "AddNewsViewController.h"
#import <MBProgressHUD.h>
#import "CompereViewModel.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>


@interface AddNewsViewController ()

@property (nonatomic ,weak) IBOutlet UITextField *phoneTextField;
@property (nonatomic ,weak) IBOutlet UITextView *contentTextView;
@property (nonatomic ,weak) IBOutlet UIButton *sendButton;

- (IBAction)sendNews:(id)sender;
- (IBAction)cancleNews:(id)sender;

@end

@implementation AddNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    RAC(_sendButton,enabled) = [RACSignal combineLatest:@[_phoneTextField.rac_textSignal,_contentTextView.rac_textSignal] reduce:^id(NSString *phone , NSString *content){
        return @([phone length]>0 && [content length]>0);
        
    }];
    
    RAC(_sendButton,tintColor) = [RACSignal combineLatest:@[_phoneTextField.rac_textSignal,_contentTextView.rac_textSignal] reduce:^id(NSString *phone , NSString *content){
        return [phone length]>0 && [content length]>0?[UIColor redColor]:[UIColor grayColor];
        
        
    }];

    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id token = [userDefaults objectForKey:@"token"];
    
    if (!token) {
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:hud];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"亲，请先登录哦";
        [hud showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }];
        
        return;
    }
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 发布新动态

- (void)sendNews:(id)sender{
    
    
    //
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id token = [userDefaults objectForKey:@"token"];
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    if (!token) {
        
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"亲，请先登录哦";
        [hud showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }];
        
        return;
    }
    
    
    
    @weakify(self);
    [[[[CompereViewModel shareCompereViewModel] addCompere:_contentTextView.text phone:_phoneTextField.text] deliverOn:[RACScheduler mainThreadScheduler]]subscribeNext:^(id x) {
        hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
        hud.labelText= @"发布成功";
        [hud showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        } onQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionBlock:^{
            @strongify(self);
            [self cancleNews:nil];
            
        }];
        
        
        
    } error:^(NSError *error) {
        
    }];
    


}

#pragma mark 取消发布

- (void)cancleNews:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
